/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.taglib.head.js;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * This tag generate a new javascript function name, and register the function
 * so that it is called at the initialization of the page.
 */
@SuppressWarnings("serial")
public class SpringJsAddFunction
    extends TagSupport
{

  /**
   * the name of the javascript function.
   */
  private String functionName = null;

  /**
   * the order in which the function must be called.
   */
  private int order = 0;

  /**
   * the {@link JsFunctionContainer} to use.
   */
  private JsFunctionContainer jsFunctionContainer;



  /**
   * Processing of the start tag.
   * 
   * @throws JspException
   *           when an error occurred
   * @return SKIP_BODY
   */
  @Override
  public int doStartTag()
      throws JspException
  {
    final JsFunctionContainer jsFunctionContainer = getJsFunctionContainer();
    String functionName = getFunctionName();
    if (functionName == null) {
      functionName = jsFunctionContainer.getFunctionsBaseName() + jsFunctionContainer.getNextFunctionIdentifier();
    }
    try {
      pageContext.getOut().write(functionName);
    }
    catch (final IOException e) {
      throw new JspException(e);
    }
    jsFunctionContainer.addFunction(functionName, getOrder());
    return SKIP_BODY;
  }



  /**
   * getter for the functionName property.
   * 
   * @return the functionName
   */
  public String getFunctionName()
  {
    return functionName;
  }



  /**
   * getter for the jsFunctionContainer property.
   * 
   * @return the jsFunctionContainer
   */
  public JsFunctionContainer getJsFunctionContainer()
  {
    return jsFunctionContainer;
  }



  /**
   * getter for the order property.
   * 
   * @return the order
   */
  public int getOrder()
  {
    return order;
  }



  /**
   * Release state.
   */
  @Override
  public void release()
  {
    super.release();
    functionName = null;
    order = 0;
  }



  /**
   * setter for the functionName property.
   * 
   * @param functionName
   *          the functionName to set
   */
  public void setFunctionName(final String functionName)
  {
    this.functionName = functionName;
  }



  /**
   * setter for the jsFunctionContainer property.
   * 
   * @param jsFunctionContainer
   *          the jsFunctionContainer to set
   */
  public void setJsFunctionContainer(JsFunctionContainer jsFunctionContainer)
  {
    this.jsFunctionContainer = jsFunctionContainer;
  }



  /**
   * setter for the order property.
   * 
   * @param order
   *          the order to set
   */
  public void setOrder(final int order)
  {
    this.order = order;
  }

}
